package leetcode.每日一题.二分查找;

import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author ：zsy
 * @date ：Created 2022/12/20 7:06 PM
 * @description：
 */
public class 袋子里最少数目的球 {
    @Test
    public void test() {
        System.out.println(new Solution().minimumSize(new int[]{9}, 2));
    }

    class Solution {
        public int minimumSize(int[] nums, int maxOperations) {
            // 最小
            int left = 1;
            // 最大
            int right = Arrays.stream(nums).max().getAsInt();
            int ans = 0;
            // 二分查找
            while (left <= right) {
                int y = left + right >> 1;
                int ops = 0;
                for (int num : nums) {
                    ops += (num - 1) / y;
                }
                if (ops <= maxOperations) {
                    ans = y;
                    right = y - 1;
                } else {
                    left = y + 1;
                }
            }
            return ans;
        }
    }
}
